/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * $Id: JPEGEncoder_example.cpp,v 1.5 2007-11-28 11:53:18 amaula Exp $
 *
 */

//*****************************************************************************
#include "JPEGEncoder.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

int main(int argc, char *argv[])
{
  CJPEGEncoder encoder;

  if (argc != 5 && argc != 4) {
    printf("Usage:\t%s <input_YUV_file> <output_JPG_file> <image_width> <image_height> - YUV to JPEG conversion\n",argv[0]);
    printf("\t%s <output_jpeg_file> <image_width> <image_height> - Generate Random image\n", argv[0]);

  } else {
    if (argc == 4) {
      srandom(time(NULL));

      // Generate "image"
      const int imgsize = 3 * atoi(argv[2]) * atoi(argv[3]);
      char *buffer = (char *)malloc(imgsize);
      int i;
      assert(buffer != NULL);
      
      for(i=0; i < imgsize; i++) {
	buffer[i] = random() & 0xFF;
      }
      
      // Set encoder ptr.
      encoder.SetYUVDataPtr(buffer, atoi(argv[2]), atoi(argv[3]));
      
      // Test
      if (encoder.GetJPEGDataPtr() != NULL) {
	FILE *out = fopen(argv[1],"wb");
	if (out) {
	  
	  // Write!
	  fwrite(encoder.GetJPEGDataPtr(), 
		 encoder.GetJPEGDataSize(), 
		 1, out);
	  fclose(out);
	  // Print
	  printf("Created JPEG file '%s', size %d x %d from Random data\n",
		 argv[1], atoi(argv[2]), atoi(argv[3]));
	} else {
	  printf("Failed to open output file '%s'!\n", argv[1]);
	  
	}
      } else {
	printf("Failed to create JPEG file '%s', Encoding failed!\n", argv[1]);
      }
    } else {
      
      char *data = NULL;
      struct stat s;
      
      if (stat(argv[1], &s) == 0) {
	
	// Allocate mem for whole YUV image.
	data = new char [s.st_size];
      
	// Read the file in memory
	FILE *in = fopen(argv[1],"rb");
	if (in) {
	  fread(data, s.st_size, 1, in);
	  fclose(in);

	  // Image dimensions
	  const int jpgW = atoi(argv[3]);
	  const int jpgH = atoi(argv[4]);
	
	  // Set encoder ptr.
	  encoder.SetYUVDataPtr(data, jpgW, jpgH);
	
	  // Write out the file
	  const char *jpgData = encoder.GetJPEGDataPtr();
	  const int jpgDataSize = encoder.GetJPEGDataSize();
	  if (jpgData) {
	    printf("Converted YUV '%s' (size: %u bytes, w:%d, h:%d) to JPEG-file '%s' (Size %d bytes)\n",
		   argv[1], (unsigned)s.st_size, jpgW, jpgH, argv[2], jpgDataSize);
	    FILE *out = fopen(argv[2],"wb");
	    if (out) {
	      fwrite(encoder.GetJPEGDataPtr(), 
		     encoder.GetJPEGDataSize(), 
		     1, out);
	      fclose(out);
	    } else {
	      printf("Failed to open output file '%s'!\n", argv[2]);
	    
	    }
	  } else {
	    printf("FAILED to convert YUV '%s' to JPEG-file '%s'\n",
		   argv[1], argv[2]);
	  
	  }
	} else {
	  printf("FAILED to fopen() input file '%s' (%s)!\n", argv[1], strerror(errno));
	}
      
	delete[] data;
      } else {
	printf("FAILED to stat() input file '%s' (%s)!\n", argv[1], strerror(errno));
      }
    }
  }
  return 0;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
